package com.service.qm;

import java.io.IOException;
import java.util.*;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.model.Global;
import com.dao.qm.ConfigsDao;
import com.model.qm.ConfigsModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.qm.PartsDetailDao;
import com.model.qm.PartsDetailModel;

@Service
public class PartsDetailService {
    static final Logger logger = LoggerFactory.getLogger(PartsDetailService.class);

    @Autowired
    PartsDetailDao partsDetailDao;
    @Autowired
    ConfigsDao configsDao;


    /**
     * 列表查询
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectList(Map<String, Object> queryMap) {
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        List<PartsDetailModel> list = partsDetailDao.selectList(queryMap);
        int count = partsDetailDao.selectListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectList:{}", list);
        return httpResult;
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    public HttpResult selectInfor(String deaId) {
        HttpResult httpResult = new HttpResult();
        PartsDetailModel partsDetailModel = partsDetailDao.load(deaId);
        httpResult.setData(partsDetailModel);
        httpResult.setCode(HttpStatus.SUCCESS_CODE);
        return httpResult;
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    public HttpResult save(PartsDetailModel partsDetailModel) {
        if (partsDetailModel == null) {
            return HttpResult.error("参数不能为空");
        }
        HttpResult httpResult = new HttpResult();
        int result = 0;
        if (!StringUtils.isNotNull(partsDetailModel.getDeaId())) {
            partsDetailModel.setDeaId(UUID.randomUUID().toString());
            result = partsDetailDao.insert(partsDetailModel);
        } else {
            result = partsDetailDao.update(partsDetailModel);
        }
        if (result > 0) {
            httpResult.setMsg(partsDetailModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(partsDetailModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(partsDetailModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    public HttpResult delete(String deaId) {
        HttpResult httpResult = new HttpResult();
        if (!StringUtils.isNotNull(deaId)) {
            return HttpResult.error("参数不能为空");
        }
        int result = partsDetailDao.delete(deaId);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 导出
     *
     * @param queryMap
     * @param response
     */
    public void export(Map<String, Object> queryMap, HttpServletResponse response) {
        List<PartsDetailModel> list = partsDetailDao.selectList(queryMap);
        logger.info("export:{}", list);
        try {
            EasyExcel.write(response.getOutputStream(), PartsDetailModel.class).sheet("模板").doWrite(list);
        } catch (IOException e) {
            logger.error("导出错误:{}", e.getMessage());
        }

    }

    /**
     * 查询各种下拉框的备选项
     */
    public List getPartsDetailOptions(String type) {
        List<Map> configs = configsDao.selectListByType(type);
        return configs;
    }
}
